#include<iostream>
#include<vector>
#include<queue>
#include<string>
using namespace std;



struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left),
        right(right) {}
};
 
class Solution {
public:
    string tree2str(TreeNode* root) {
        string answer;
        if (root == nullptr)
            return answer;

        answer += to_string(root->val);

        if (root->left != nullptr || root->right)
        {
            answer += '(';
            answer += tree2str(root->left);
            answer += ")";
        }


        //if(root->right)

        if (root->right != nullptr)
        {
            answer += '(';
            answer += tree2str(root->right);
            answer += ")";
        }


        return answer;
    }
};